package com.neu.ui.home;

import android.app.DownloadManager;
import android.app.VoiceInteractor;

import com.android.volley.Request;
import com.google.gson.Gson;
import com.neu.bean.AllService;
import com.neu.bean.Rotation;
import com.neu.bean.Weather;
import com.neu.util.NetUtil;
import com.neu.util.VolleyUtil;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class HomeViewModel extends ViewModel {

    private MutableLiveData<Rotation> mRotation;
    private MutableLiveData<Weather> mWeather;
    private MutableLiveData<AllService> mAllService;
    private Gson gson;

    public HomeViewModel() {
        mRotation = new MutableLiveData<>();
        mWeather = new MutableLiveData<>();
        mAllService = new MutableLiveData<>();
        gson=new Gson();
    }

    public LiveData<AllService> getAllService(){
        String url="/prod-api/api/service/list";
        VolleyUtil.request(Request.Method.GET, url, null, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                AllService allService=gson.fromJson(json,AllService.class);
                mAllService.setValue(allService);
            }
        });

        return mAllService;
    }

    /**
     * 获得当日天气
     * @return
     */
    public LiveData<Weather> getWeather(){
        String url="/prod-api/api/common/weather/today";
        VolleyUtil.request(Request.Method.GET, url, null, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                Weather weather=gson.fromJson(json,Weather.class);
                mWeather.setValue(weather);
            }
        });

        return mWeather;
    }

    /**
     * 获得轮播图的数据
     * @return
     */
    public LiveData<Rotation> getRotation(){
        String url="/prod-api/api/rotation/list?pageNum=1&pageSize=8&type=2";
        VolleyUtil.request(Request.Method.GET, url, null, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                //从网络中来的数据
                Rotation rotation=gson.fromJson(json,Rotation.class);
                mRotation.setValue(rotation);
            }
        });


        return mRotation;
    }

}